
package com.fsoft.core.dict.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fsoft.core.common.QueryParam;
import com.fsoft.core.common.base.service.impl.BaseServiceImpl;
import com.fsoft.core.common.exceptions.RRException;
import com.fsoft.core.dict.entity.SysDictItem;
import com.fsoft.core.dict.mapper.SysDictItemMapper;
import com.fsoft.core.dict.service.SysDictItemService;
import com.fsoft.core.utils.DateTimeUtils;
import com.fsoft.core.utils.OgnlUtils;
import com.fsoft.core.utils.tree.BuildTree;
import com.fsoft.core.utils.tree.Tree;

@Transactional
@Service("sysDictItemsService")
public class SysDictItemsServiceImpl extends BaseServiceImpl<SysDictItem> implements SysDictItemService {
	@Autowired
	private SysDictItemMapper mapper;

	@Override
	public int removeByDict(String dictId) throws Exception {
		return mapper.deleteByDict(dictId);
	}

	@Override
	public int removeByDict(List<String> list) throws Exception {
		return mapper.deleteBatch(list);
	}

	@Override
	public List<SysDictItem> findList(String dictCode) throws Exception {
		SysDictItem item = new SysDictItem();
		item.setDictCode(dictCode);
		QueryParam params = new QueryParam(item);
		return findList(params);
	}

	@Override
	public List<Tree> findTrees(QueryParam param) throws Exception {
		List<SysDictItem> list = findList(param);
		return BuildTree.buildTree(list);
	}

	@Override
	public List<Tree> findTrees(String dictCode) throws Exception {
		QueryParam param = new QueryParam();
		param.put("dictCode", dictCode);
		return findTrees(param);
	}

	@Override
	public SysDictItem getEntity(String dictCode, String itemCode) throws Exception {
		SysDictItem item = new SysDictItem();
		item.setDictCode(dictCode);
		item.setCode(itemCode);
		QueryParam params = new QueryParam(item);
		List<SysDictItem> items = findList(params);
		return OgnlUtils.isEmpty(items) ? null : items.get(0);
	}

	@Override
	public int save(SysDictItem entity) throws Exception {
		if (StringUtils.isBlank(entity.getDictId()))
			throw new RRException("字典标识dictId为空!");
		if (StringUtils.isBlank(entity.getCode()))
			throw new RRException("配置项编号为空!");
		SysDictItem item = getEntity(entity.getDictId(), entity.getCode());
		if (OgnlUtils.isNotEmpty(item))
			throw new RRException("配置项编号[" + entity.getCode() + "]已被占用，请重新输入");
		if (StringUtils.isBlank(entity.getParentId()))
			entity.setParents(entity.getId());
		else {
			SysDictItem p_item = getEntity(entity.getParentId());
			entity.setParents(p_item.getParents() + "_" + entity.getId());
		}
		return super.save(entity);
	}

	@Override
	public int modify(SysDictItem entity) throws Exception {
		if (StringUtils.isBlank(entity.getId()))
			throw new RRException("id标识为空!");
		if (StringUtils.isBlank(entity.getDictId()) && StringUtils.isNotBlank(entity.getCode()))
			throw new RRException("字典标识dictId为空!");
		if (StringUtils.isNotBlank(entity.getDictId()) && StringUtils.isNotBlank(entity.getCode())) {
			// 编辑了配置项编号
			SysDictItem item = getEntity(entity.getDictId(), entity.getCode());
			if (OgnlUtils.isNotEmpty(item) && !StringUtils.equals(entity.getId(), item.getId()))
				throw new RRException("配置项编号[" + entity.getCode() + "]已被占用，请重新输入");
		}
		if (OgnlUtils.isEmpty(entity.getModifyTime()))
			entity.setModifyTime(DateTimeUtils.getNowTime());
		return super.modify(entity);
	}

}
